$(function (){
    //tab开关
    $(".ad_change").on("click",function (){
        $('.add_show_list1').hide();
        $('.add_show_list2').show();
    })
     //取消
    $(".remove_ad").click(function (){
        location.reload();
    })
    //点击保存
    $(".add_ad").on("click", function () {
        //alert(1);
        var kq_qdy = $(".kq_qdy").prop("checked");//开启启动页
        var kq_lunbotu = $(".kq_lunbotu").prop("checked");//开启轮播图
        var kq_zhuanti = $(".kq_zhuanti").prop("checked");//开启专题
        var kq_wzdb = $(".kq_wzdb").prop("checked");//开启文章底部
        var zdy_add = $(".zdy_add").prop("checked");//开启自定义广告
        var tz_url = $(".tz_url").val();//跳转链接url
        var arcImg = $(".tu img").attr("fsrc") || "";//封面图
        if (arcImg==""){
            zeroModal.alert('请选择封面图片');
            return false;
        }
        if (tz_url==""){
            zeroModal.alert('请输入跳转url');
            return false;
        }
        var json={
            // id:0,
            // abottom: kq_wzdb,
            // banner: kq_lunbotu,
            // diystart: zdy_add,
            // image: arcImg,
            // smiddle: kq_zhuanti,
            // startup: kq_qdy,
            // url: tz_url

        };
        var result = JSON.stringify(json);
        zeroModal.loading(5);
        $.ajax({
            url: baseNews + "/api/data/sys/avcfg/update/?id" + `=${0}&startup=${kq_qdy}&banner=${kq_lunbotu}&smiddle=${kq_zhuanti}&abottom=${kq_wzdb}&diystart=${zdy_add}&url=${tz_url}&image=${arcImg}`,
            type: "post",
            headers: { 'Content-Type': 'application/json' },
            dataType: "json",
            data: result,
            success: function (res) {
                console.log(res, 666)
                if (res.code == 0) {
                    location.reload();
                } else {

                    zeroModal.alert('添加失败');

                }
            }
        })
    })
    $.ajax({
        url: baseNews + "/api/data/sys/avcfg/info/",
        success:function (res){
            console.log(res,444);
            if(res.code==0){
                var key=res.data;
                if(key.startup){
                    $(".kq_qdy").prop("checked","checked");
                }
                if(key.banner){
                    $(".kq_lunbotu").prop("checked","checked");
                }
                if(key.smiddle){
                    $(".kq_zhuanti").prop("checked","checked");
                }
                if(key.abottom){
                    $(".kq_wzdb").prop("checked","checked");//
                }
                if(key.diystart){
                    $(".zdy_add").prop("checked","checked");
                }
                if(key.url){
                    $(".tz_url").val(key.url);
                }
                if(key.image){
                    $(".tu img").attr("src",key.image);
                    $(".tu img").attr("fsrc", key.image);
                    
                }
                var arcImg = $(".tu img").attr("fsrc") || "";//封面图

                var html =`<tr>
                        <td  width="5%"><input type="checkbox" /></td>
                        <td width="5%">${key.id}</td>
                        <td >${key.startup?'是':'否'}</td>
                        <td >${key.banner?'是':'否'}</td>
                        <td >${key.smiddle?'是':'否'}</td>
                        <td >${key.abottom?'是':'否'}</td>
                        <td >${key.diystart ?'是':'否'}</td>
                        <td >${key.url}</td>
                        <td ><img style=" max-width: 160px;
            max-height: 160px;
            display: block;
            margin: 0 auto;" src="${key.image}" /></td>
                    </tr>`;
                $('.ad_list').append(html);
            }
        }
    })
    $('.file').on('change', function (e) {
        var e = e || event;
        var reader = new FileReader();
        console.log(reader)
        reader.readAsDataURL($('.file')[0].files[0]);//发起异步请
        //上传图片1
        sendImg('.tu img', $('.file')[0].files[0])
        reader.onload = function () {
            var src = this.result
            $('.tu img').attr('src', src)
        }
    })
})
// 图片上传的。。
function sendImg(obj, msrc) {
    var formData = new FormData();
    formData.append('file', msrc);
    $.ajax({
        url: baseNews + '/oss/upload/img',
        data: formData,
        type: "post",
        cache: false,
        processData: false,
        contentType: false,
        success: function (res) {
            console.log(res);
            if (res.code == 0) {
                $(obj).attr("fsrc", res.data);
            } else {
                alert("上传失败");
                return false;
            }
        }

    })
}
